package com.liusuai.yixian.WebSocket;

import com.liusuai.yixian.login.UserBean;
import com.liusuai.yixian.login.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Component
public class WebSocketService {

    @Autowired //自动连接到UserService Bean
    private UserMapper userMapper;

    @Autowired //自动连接到UserService Bean
    private MessageMapper messageMapper;

    public void updateSocketId(String socketId,String userId){
        userMapper.updateSocketId(socketId,userId);
    }

    public UserBean getUserByID(String userId) {
        return userMapper.selectUserByID(userId);
    }

    public List<UserBean> getUserByIds(String[] ids) {
        ArrayList<UserBean> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            UserBean userByID = getUserByID(ids[i]);
            if (userByID != null) {
                list.add(userByID);
            }

        }
        return list;
    }

    public void saveUnsendMessage(UnSendMsgBean unSendMessage) {
        messageMapper.insertUnSend(unSendMessage);
    }

    public List<UnSendMsgBean> getUnGotMsg(String toId) {
        return messageMapper.getUnSendMessageById(toId);
    }
    public void delUnGotMsgS(String toId) {
         messageMapper.delUnSendMsgs(toId);
    }
}
